package com.chinatgl.latte.ec.sign;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chinatgl.latte.delegates.LatteDelegate;
import com.chinatgl.latte.ec.API;
import com.chinatgl.latte.ec.R;
import com.chinatgl.latte.ec.R2;
import com.chinatgl.latte.ec.params.MapUtil;
import com.chinatgl.latte.net.NetRequest;
import com.chinatgl.latte.net.listener.onNetRquestListener;

import java.util.WeakHashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文件描述:
 * 作者: Created by 杨伟乔 on 2017/8/8 0008.
 * 版本号: 1.0
 */

public class SignUpDelegate extends LatteDelegate {
    @BindView(R2.id.ll_back)
    LinearLayout fragRegisterTvBack;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    @BindView(R2.id.act_home_et_phone)
    AutoCompleteTextView actHomeEtPhone;
    @BindView(R2.id.frag_register_tv_getcode)
    TextView fragRegisterTvGetcode;
    @BindView(R2.id.act_home_et_sms_code)
    AutoCompleteTextView actHomeEtSmsCode;
    @BindView(R2.id.act_reg_et_pwd)
    AutoCompleteTextView actRegEtPwd;
    @BindView(R2.id.act_reg_et_pwd2)
    AutoCompleteTextView actRegEtPwd2;
    @BindView(R2.id.act_reg_tv_reg)
    TextView actRegTvReg;
    @BindView(R2.id.ll_xieyi)
    LinearLayout llXieyi;
    @BindView(R2.id.ll_password1)
    LinearLayout llPassword1;
    @BindView(R2.id.ll_password2)
    LinearLayout llPassword2;
    @BindView(R2.id.tv_xieyi)
    TextView tvXieyi;

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_up;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }

    /**
     * 点击事件
     */

    @OnClick({R2.id.ll_back, R2.id.tv_xieyi, R2.id.frag_register_tv_getcode, R2.id.act_reg_tv_reg})
    public void onClick(View view) {
        switch (view.getId()) {

            case R2.id.tv_xieyi:
//            Intent intent = new Intent(this, XieYiActivity.class);
//            startActivity(intent);
                break;


            case R2.id.ll_back:
//            ActivityFinishUtils.removeActivity(this);
//            //返回按钮
//            finish();
                break;
            case R2.id.frag_register_tv_getcode:
                //发送验证码  获取验证码
                /**
                 * 发送验证码 验证手机号码
                 */
                if (checkPhone()) {
                    getCode();
                }
//            getDatasFromView();
//
//            if (!StringUtils.isMobileNO(phone)) {
//                Toastutils.showToastCenter("手机格式不正确");
//                return;
//            }
//
//            if (intExtra == Contains.BANDINGPHONE) {
//                //检查手机号 是否注册
//                ArrayMap<String, String> map = new ArrayMap<>();
//                map.put("username", phone);
//                map.put("thirdLoginType", "1");
//                Type type = new TypeToken<Result>() {
//                }.getType();
//                NetRequest.postForparams(Api.CHECKUSERNAME, map, HTTP_TASK_KEY, type, this, this, CacheMode.NO_CACHE);
//            } else {
//                getCode();
//
//            }


                break;
            case R2.id.act_reg_tv_reg:


                if (checkForm()) {

                }
//            //提交按钮
//            /**获取控件上的数据**/
//            /**判断控件上的数据是否填写完整*/
//            /**提交数据 **/
//            getDatasFromView();
//            //检查数据通过 提交数据
//            if (checkDatas()) {
//                ArrayMap<String, String> map = new ArrayMap<>();
//                map.put("username", phone);
//                map.put("code", smsCode);
//
//                String url = null;
//                Type type = null;
//
//                switch (intExtra) {
//                    case Contains.BANDINGPHONE:
//                        map.put("thirdId", thirdLoginId);
//                        url = Api.BINDMOBI;
//                        if (types == 0) {
//                            map.put("password", StringUtils.toMD5(password));
//                        } else {
//                            map.put("password", "");
//                        }
//
//                        type = new TypeToken<Result<UserInfo>>() {
//                        }.getType();
//                        break;
//                    case Contains.REGISTER_TYPE:
//                        //注册
//                        Toastutils.showToastCenter("注册");
//                        url = Api.REG;
//                        map.put("password", StringUtils.toMD5(password));
//                        type = new TypeToken<Result<UserInfo>>() {
//                        }.getType();
//                        break;
//                    case Contains.FORGOT_PASSWORD_TYPE:
//                        //忘记密码
//                    case Contains.RESET_PASSWORD_TYPE:
//                        //重置密码
//                        url = Api.RESETPASSWORD;
//                        map.put("password", StringUtils.toMD5(password));
//                        type = new TypeToken<Result>() {
//                        }.getType();
//                        Toastutils.showToastCenter("重置密码");
//                        break;
//
//                }
//                showLoading("请稍等...");
//                NetRequest.postForparams(url, map, HTTP_TASK_KEY, type, this, this, CacheMode.NO_CACHE);
//            }
                break;
        }
    }

    private boolean checkPhone() {
        //手机号
        final String phone = actHomeEtPhone.getText().toString().trim();
        boolean isPass = true;
        if (phone.isEmpty() || phone.length() != 11) {
            actHomeEtPhone.setError("请输入正确的手机号码");
            isPass = false;
        } else {
            actHomeEtPhone.setError(null);
        }
        return isPass;
    }

    /**
     * 获取验证码
     */
    private void getCode() {
        showGetVerificationCodeState();
        WeakHashMap<String, Object> map = MapUtil.getParams();
        map.put("mobi", actHomeEtPhone.getText().toString().trim());
        map.put("type", "1");


        NetRequest.post(API.GETCODE, map, null, false, 0, new onNetRquestListener() {
            @Override
            public void requestdataFinish(String catalog, Object bean) {

            }

            @Override
            public void requestdataFailed(String msg, Throwable e) {

            }

            @Override
            public void requestComplete() {

            }
        },getActivity());


    }

    /**
     * 提取控件上的数据
     **/
    private boolean checkForm() {
        //手机号
        final String phone = actHomeEtPhone.getText().toString().trim();
        //短信验证码
        final String smsCode = actHomeEtSmsCode.getText().toString().trim();
        //密码1
        final String password = actRegEtPwd.getText().toString().trim();
        //密码2
        final String password2 = actRegEtPwd2.getText().toString().trim();
        boolean isPass = true;
        if (phone.isEmpty() || phone.length() != 11) {
            actHomeEtPhone.setError("请输入正确的手机号码");
            isPass = false;
        } else {
            actHomeEtPhone.setError(null);
        }
        if (smsCode.isEmpty()) {
            actHomeEtSmsCode.setError("请输入验证码");
            isPass = false;
        } else {
            actHomeEtSmsCode.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            actRegEtPwd.setError("请填写至少6位数的密码");
            isPass = false;
        } else {
            actRegEtPwd.setError(null);
        }
        if (password2.isEmpty() || !password.equals(password2)) {
            actRegEtPwd2.setError("两次密码输入不一致");
            isPass = false;
        } else {
            actRegEtPwd2.setError(null);
        }

        return isPass;
    }

    private static final int TIME_INTERVAL = 60;
    private int one_minute = TIME_INTERVAL;
    private Handler handler = new Handler();

    public void showGetVerificationCodeState() {

        fragRegisterTvGetcode.setEnabled(false);
        handler.post(new Runnable() {
            @Override
            public void run() {
                fragRegisterTvGetcode.setText(--one_minute < 0 ? "验证码" :
                        String.valueOf(one_minute).concat("秒"));
                if (one_minute < 0) {
                    one_minute = TIME_INTERVAL;
                    fragRegisterTvGetcode.setEnabled(true);
                    return;
                }
                handler.postDelayed(this, 1000);
            }
        });
    }
}
